\section{Source/TopDownView.c}


\begin{footnotesize}\begin{alltt}
00001 \textcolor{preprocessor}{#include "TopDownView.h"}
00002 
00003 \textcolor{keywordtype}{int} TileWidth, TileHeight, AreaWidth, AreaHeight, MapWidth, MapHeight;
00004 \textcolor{keywordtype}{int} AvatarX, AvatarY; SDL\_Surface *AvatarImage;
00005 
00006 SDL\_Surface *DisplayArea;
00007 
00008 SDL\_Surface **TileArray;
00009 \textcolor{keywordtype}{int} TileArraySize;
00010 
00011 SDL\_Surface *AvatarImages[8];
00012 
00013 \textcolor{keywordtype}{int} *DisplayMap;
00014 
00015 \textcolor{keywordtype}{void} TDV\_SetTileWidth(\textcolor{keywordtype}{int} Width)
00016 \{
00017     TileWidth = Width;
00018 \}
00019 \textcolor{keywordtype}{void} TDV\_SetTileHeight(\textcolor{keywordtype}{int} Height)
00020 \{
00021     TileHeight = Height;
00022 \}
00023 \textcolor{keywordtype}{void} TDV\_SetTileDimensions(\textcolor{keywordtype}{int} Width, \textcolor{keywordtype}{int} Height)
00024 \{
00025     TileWidth = Width;
00026     TileHeight = Height;
00027 \}
00028 \textcolor{keywordtype}{void} TDV\_ChangeAreaWidth(\textcolor{keywordtype}{int} Width)
00029 \{
00030     AreaWidth = Width;
00031 \}
00032 \textcolor{keywordtype}{void} TDV\_ChangeAreaHeight(\textcolor{keywordtype}{int} Height)
00033 \{
00034     AreaHeight = Height;
00035 \}
00036 \textcolor{keywordtype}{void} TDV\_ChangeAreaDimensions(\textcolor{keywordtype}{int} Width, \textcolor{keywordtype}{int} Height)
00037 \{
00038     AreaWidth = Width;
00039     AreaHeight = Height;
00040 \}
00041 \textcolor{keywordtype}{void} TDV\_SetAvatarX(\textcolor{keywordtype}{int} X)
00042 \{
00043     AvatarX = X;
00044 \}
00045 \textcolor{keywordtype}{void} TDV\_SetAvatarY(\textcolor{keywordtype}{int} Y)
00046 \{
00047     AvatarY = Y;
00048 \}
00049 \textcolor{keywordtype}{void} TDV\_SetAvatarPosition(\textcolor{keywordtype}{int} X, \textcolor{keywordtype}{int} Y)
00050 \{
00051     AvatarX = X;
00052     AvatarY = Y;
00053 \}
00054 \textcolor{keywordtype}{void} TDV\_SetMapWidth(\textcolor{keywordtype}{int} Width)
00055 \{
00056     MapWidth = Width;
00057 \}
00058 \textcolor{keywordtype}{void} TDV\_SetMapHeight(\textcolor{keywordtype}{int} Height)
00059 \{
00060     MapHeight = Height;
00061 \}
00062 \textcolor{keywordtype}{void} TDV\_SetMapDimensions(\textcolor{keywordtype}{int} Width, \textcolor{keywordtype}{int} Height)
00063 \{
00064     MapWidth = Width;
00065     MapHeight = Height;
00066 \}
00067 \textcolor{keywordtype}{int} TDV\_GetTileWidth()
00068 \{
00069     \textcolor{keywordflow}{return} TileWidth;
00070 \}
00071 \textcolor{keywordtype}{int} TDV\_GetTileHeight()
00072 \{
00073     \textcolor{keywordflow}{return} TileHeight;
00074 \}
00075 \textcolor{keywordtype}{int} TDV\_GetAreaWidth()
00076 \{
00077     \textcolor{keywordflow}{return} AreaWidth;
00078 \}
00079 \textcolor{keywordtype}{int} TDV\_GetAreaHeight()
00080 \{
00081     \textcolor{keywordflow}{return} AreaHeight;
00082 \}
00083 \textcolor{keywordtype}{int} TDV\_GetAvatarX()
00084 \{
00085     \textcolor{keywordflow}{return} AvatarX;
00086 \}
00087 \textcolor{keywordtype}{int} TDV\_GetAvatarY()
00088 \{
00089     \textcolor{keywordflow}{return} AvatarY;
00090 \}
00091 \textcolor{keywordtype}{int} TDV\_GetMapHeight()
00092 \{
00093     \textcolor{keywordflow}{return} MapHeight;
00094 \}
00095 \textcolor{keywordtype}{int} TDV\_GetMapWidth()
00096 \{
00097     \textcolor{keywordflow}{return} MapWidth;
00098 \}
00099 
00100 \textcolor{keywordtype}{int} TDV\_InitTopDownView(\textcolor{keywordtype}{int} Width, \textcolor{keywordtype}{int} Height, SDL\_Surface *Display)
00101 \{
00102     \textcolor{comment}{/* Initialise the things needed for use in this interface */}
00103     AreaWidth = Width;
00104     AreaHeight = Height;
00105     DisplayArea = Display;
00106     TileArray = malloc(10*\textcolor{keyword}{sizeof}(TileArray));
00107     \textcolor{keywordflow}{if}(TileArray == NULL) \textcolor{keywordflow}{return} -1;
00108     TileArraySize = 10;
00109     \textcolor{keywordflow}{return} 0;
00110 \}
00111 
00112 \textcolor{keywordtype}{int} TDV\_SetTileFromFile(\textcolor{keywordtype}{int} Reference, \textcolor{keywordtype}{char} *Filename)
00113 \{
00114     \textcolor{keywordflow}{if}(Reference >= TileArraySize)
00115     \{
00116         TileArraySize += TileArraySize+Reference;
00117         \textcolor{keywordtype}{void} *TmpPointer = realloc(TileArray, TileArraySize*\textcolor{keyword}{sizeof}(TileArray));
00118         \textcolor{keywordflow}{if}(TmpPointer == NULL) \textcolor{keywordflow}{return} -1;
00119         TileArray = TmpPointer;
00120     \}
00121     \textcolor{comment}{/* Getting a casting error when setting TileArray[Reference] = IMG\_Load(..);}
00122 \textcolor{comment}{        Changed it for this which works */}
00123     SDL\_Surface *Tmp = IMG\_Load(Filename);
00124     \textcolor{keywordflow}{if}(Tmp == NULL) \textcolor{keywordflow}{return} -1;
00125     TileArray[Reference] = Tmp;
00126     \textcolor{keywordflow}{return} Reference;
00127 \}
00128 
00129 
00130 \textcolor{keywordtype}{int} TDV\_SetTileFromSurface(\textcolor{keywordtype}{int} Reference, SDL\_Surface *Surface)
00131 \{
00132     \textcolor{keywordflow}{if}(Reference >= TileArraySize)  
00133     \{
00134         TileArraySize += TileArraySize + Reference;
00135         \textcolor{keywordtype}{void} *TmpPointer = realloc(TileArray, TileArraySize*\textcolor{keyword}{sizeof}(TileArray));
00136         \textcolor{keywordflow}{if}(TmpPointer == NULL) \textcolor{keywordflow}{return} -1;
00137         TileArray = TmpPointer;
00138     \}
00139     TileArray[Reference] = Surface;
00140     \textcolor{keywordflow}{return} Reference;
00141 \}
00142 
00143 \textcolor{keywordtype}{int} TDV\_SetAvatarImage(SDL\_Surface *Image)
00144 \{
00145     AvatarImage = Image;
00146     \textcolor{keywordflow}{return} 0;
00147 \}
00148 
00149 
00150 \textcolor{keywordtype}{void} TDV\_DrawToArea()
00151 \{
00152     \textcolor{comment}{/* Put the background of the area as black */}
00153     SDL\_Rect R;
00154     R.x = 0;
00155     R.y = 0;
00156     R.w = AreaWidth;
00157     R.h = AreaHeight;
00158     SDL\_FillRect(DisplayArea, &R, 0x000000);
00159     
00160     \textcolor{comment}{/* Work out dimensions and whatnot */}
00161     \textcolor{keywordtype}{int} TilesPerRow = AreaWidth/TileWidth;
00162     \textcolor{keywordtype}{int} TilesPerColumn = AreaHeight/TileHeight;
00163     \textcolor{keywordtype}{int} X, Y;
00164     \textcolor{keywordtype}{int} MapXUpper = AvatarX + (TilesPerRow/2);
00165     \textcolor{keywordtype}{int} MapXLower = AvatarX - (TilesPerRow/2);
00166     \textcolor{keywordtype}{int} MapYUpper = AvatarY + (TilesPerColumn/2);
00167     \textcolor{keywordtype}{int} MapYLower = AvatarY - (TilesPerColumn/2);
00168     \textcolor{comment}{/* Actually draw it */}
00169     \textcolor{keywordflow}{for}(X = MapXLower; X < MapXUpper; ++X)
00170     \{   
00171         \textcolor{keywordflow}{for}(Y = MapYLower; Y < MapYUpper; ++Y)
00172         \{
00173             SDL\_Surface *CurrentTile = TDV\_I\_GetTileAt(X, Y);
00174             \textcolor{keywordflow}{if}(CurrentTile != NULL) \textcolor{comment}{/* If the tile actually exists */}
00175             \{
00176                 R.x = TileWidth*(X-MapXLower);
00177                 R.y = TileHeight*(Y-MapYLower);
00178                 R.w = TileWidth;
00179                 SDL\_BlitSurface(CurrentTile, NULL, DisplayArea, &R);
00180             \}
00181         \}
00182     \}
00183 \}
00184 
00185 SDL\_Surface *TDV\_I\_GetTileAt(\textcolor{keywordtype}{int} X, \textcolor{keywordtype}{int} Y)
00186 \{
00187     \textcolor{comment}{/* Check the bounds */}
00188     \textcolor{keywordflow}{if}(X < 0 || X >= MapWidth)
00189         \textcolor{keywordflow}{return} NULL;
00190     \textcolor{keywordflow}{if}(Y < 0 || Y >= MapHeight)
00191         \textcolor{keywordflow}{return} NULL;
00192     \textcolor{keywordflow}{return} TileArray[DisplayMap[X*MapHeight + Y]];
00193 \}
00194 
00195 \textcolor{keywordtype}{void} TDV\_SetMap(\textcolor{keywordtype}{int} *Map)
00196 \{
00197     DisplayMap = Map;
00198 \}
00199 
00200 \textcolor{keywordtype}{void} TDV\_DrawAvatar()
00201 \{
00202     \textcolor{comment}{/* Draw the player image on to the middle of the area */}
00203     SDL\_Rect R;
00204     R.x = AreaWidth/2;
00205     R.y = AreaHeight/2;
00206     SDL\_BlitSurface(AvatarImage, NULL, DisplayArea, &R);
00207 \}
\end{alltt}\end{footnotesize}
